//
//  CategorySelectGridView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 16.04.25.
//

import SwiftUI

struct CategorySelectGridView: View {
    
    @StateObject var viewModel = CategorySelectGridViewModel()
    
    var onCategoryClick:(AccountingCategory)->Void
    
    var disableCategoryList: Array<AccountingCategory>? = nil
    
    let columns = [
        GridItem(.flexible(), alignment: .center),
        GridItem(.flexible(), alignment: .center),
        GridItem(.flexible(), alignment: .center),
        GridItem(.flexible(), alignment: .center),
        GridItem(.flexible(), alignment: .center),
    ]
    var body: some View {
        ScrollView {
            VStack {
                Text("选择类别")
                    .font(.system(size: 16))
                    .fontWeight(.medium)
                    .foregroundColor(Color.koiColorTitle)
                    .padding(.vertical, 25)
                
                LazyVGrid(columns: columns, spacing: 16) {
                    ForEach(viewModel.categoryList, id: \.self.id) { category in
                        Button(action: {
                            onCategoryClick(category)
                        }) {
                            CategoryGridItemView(category: category)
                        }
                    }
                }
                Spacer()
            }
        }
        .background(Color.white)
        .cornerRadius(16)
        .onAppear {
            viewModel.initDisableCategoryMap(disableCategoryList: disableCategoryList)
            viewModel.loadCategorys()
        }
    }
}

#Preview {
    CategorySelectGridView(onCategoryClick: { _ in})
}
